from flask import Flask,render_template
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
a1 = 22
a2 = 27
b1 = 25
b2 = 24
pwma = 18
pwmb = 23
GPIO.setup(a1, GPIO.OUT)
GPIO.setup(a2, GPIO.OUT)
GPIO.setup(pwma, GPIO.OUT)
GPIO.setup(b1, GPIO.OUT)
GPIO.setup(b2, GPIO.OUT)
GPIO.setup(pwmb, GPIO.OUT)
pwma1 = GPIO.PWM(pwma,600)#设置pwm
pwmb1 = GPIO.PWM(pwmb,600)#设置pwm
pwma1.start(100)
pwmb1.start(100)
#以下为python源程序
#输入通道与角度。即可选通并使该通道的舵机转动到相应的角度
import time
# Import the PCA9685 module.
from Adafruit_PCA9685 import PCA9685  #导入Adafruit_PCA9685模块
# Uncomment to enable debug output.
#import logging
#logging.basicConfig(level=logging.DEBUG)					#调试打印日志输出
# Initialise the PCA9685 using the default address (0x40).
pwm = PCA9685()							#把Adafruit_PCA9685.PCA9685()引用地址赋给PWM标签
# Alternatively specify a different address and/or bus:
#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)
#2^12精度  角度转换成数值  #angle输入的角度值(0--180)  #pulsewidth高电平占空时间(0.5ms--2.5ms)   #/1000将us转换为ms  #20ms时基脉冲(50HZ)
#pulse_width=((angle*11)+500)/1000;  //将角度转化为500(0.5)<-->2480(2.5)的脉宽值(高电平时间)   angle=180时  pulse_width=2480us(2.5ms)
#date/4096=pulse_width/20 ->有上pulse_width的计算结果得date=4096*( ((angle*11)+500)/1000 )/20   -->int date=4096((angle*11)+500)/20000;
i = 0
j = 0 
go = 1
def set_servo_angle(channel, angle):					#输入角度转换成12^精度的数值
	date=4096*((angle*11)+500)/20000				#进行四舍五入运算 	date=int(4096*((angle*11)+500)/(20000)+0.5)	
	pwm.set_pwm(channel, 0, int(date))
set_servo_angle(0, 25)
set_servo_angle(1, 100)
# Set frequency to 50hz, good for servos.
pwm.set_pwm_freq(50)
app=Flask(__name__)
@app.route("/")
def main():
	
	return render_template('car.html')

###############摄像头左转##################
@app.route("/video_left")
def video_left():
	global i
	i = i + 1
	if i > 22:
		i = 22
	set_servo_angle(1, 100+5*i)
	return render_template('car.html')
	
###############摄像头右转##################
@app.route("/video_right")
def video_right():
	global i 
	i = i - 1
	if i < -19:
		i = -19
	set_servo_angle(1, 100+5*i)
	return render_template('car.html')

###############摄像头向上##################
@app.route("/video_up")
def video_up():
	global j 
	j = j - 1
	if j < 0:
		j = 0
	set_servo_angle(0, 25+5*j)
	return render_template('car.html')

###############摄像头向下##################
@app.route("/video_down")
def video_down():
	global j 
	j = j + 1
	if j > 14:
		j = 14
	set_servo_angle(0, 25+5*j)
	return render_template('car.html')

###############摄像头复位##################
@app.route("/video_reset")
def video_reset():
	set_servo_angle(0, 25)
	set_servo_angle(1, 100)
	i = 0
	j = 0
	return render_template('car.html')

###############前进##################
@app.route("/car_go")
def car_go():
	global go
	go = 1
	while go==1:
		GPIO.output(a1, GPIO.HIGH)
		GPIO.output(a2, GPIO.LOW)
		pwma1.ChangeDutyCycle(60)
		GPIO.output(b1, GPIO.HIGH)
		GPIO.output(b2, GPIO.LOW)
		pwmb1.ChangeDutyCycle(60)
		time.sleep(1)
		go = 0
	GPIO.output(a1, GPIO.LOW)
	GPIO.output(a2, GPIO.LOW)
	pwma1.ChangeDutyCycle(60)
	GPIO.output(b1, GPIO.LOW)
	GPIO.output(b2, GPIO.LOW)
	pwmb1.ChangeDutyCycle(60)
	return render_template('car.html')

###############后退##################
@app.route("/car_back")
def car_back():
	global go
	go = 1
	while go==1:
		GPIO.output(a2, GPIO.HIGH)
		GPIO.output(a1, GPIO.LOW)
		pwma1.ChangeDutyCycle(60)
		GPIO.output(b2, GPIO.HIGH)
		GPIO.output(b1, GPIO.LOW)
		pwmb1.ChangeDutyCycle(60)
		time.sleep(1)
		go = 0
	GPIO.output(a1, GPIO.LOW)
	GPIO.output(a2, GPIO.LOW)
	pwma1.ChangeDutyCycle(60)
	GPIO.output(b1, GPIO.LOW)
	GPIO.output(b2, GPIO.LOW)
	pwmb1.ChangeDutyCycle(60)
	return render_template('car.html')

###############左转##################
@app.route("/car_left")
def car_left():
	global go
	go = 1
	while go==1:
		GPIO.output(a1, GPIO.HIGH)
		GPIO.output(a2, GPIO.LOW)
		pwma1.ChangeDutyCycle(40)
		GPIO.output(b1, GPIO.HIGH)
		GPIO.output(b2, GPIO.LOW)
		pwmb1.ChangeDutyCycle(80)
		time.sleep(1)
		go = 0
	GPIO.output(a1, GPIO.LOW)
	GPIO.output(a2, GPIO.LOW)
	pwma1.ChangeDutyCycle(60)
	GPIO.output(b1, GPIO.LOW)
	GPIO.output(b2, GPIO.LOW)
	pwmb1.ChangeDutyCycle(60)
	return render_template('car.html')

###############右转##################	
@app.route("/car_right")
def car_right():
	global go
	go = 1
	while go==1:
		GPIO.output(a1, GPIO.HIGH)
		GPIO.output(a2, GPIO.LOW)
		pwma1.ChangeDutyCycle(80)
		GPIO.output(b1, GPIO.HIGH)
		GPIO.output(b2, GPIO.LOW)
		pwmb1.ChangeDutyCycle(40)
		time.sleep(1)
		go = 0
	GPIO.output(a1, GPIO.LOW)
	GPIO.output(a2, GPIO.LOW)
	pwma1.ChangeDutyCycle(60)
	GPIO.output(b1, GPIO.LOW)
	GPIO.output(b2, GPIO.LOW)
	pwmb1.ChangeDutyCycle(60)
	return render_template('car.html')

###############停车##################
@app.route("/car_stop")
def car_stop():
	GPIO.output(a1, GPIO.LOW)
	GPIO.output(a2, GPIO.LOW)
	pwma1.ChangeDutyCycle(60)
	GPIO.output(b1, GPIO.LOW)
	GPIO.output(b2, GPIO.LOW)
	pwmb1.ChangeDutyCycle(60)
	return render_template('car.html')

###############直行##################	
@app.route("/straight_line")
def straight_line():
	GPIO.output(a1, GPIO.HIGH)
	GPIO.output(a2, GPIO.LOW)
	pwma1.ChangeDutyCycle(60)
	GPIO.output(b1, GPIO.HIGH)
	GPIO.output(b2, GPIO.LOW)
	pwmb1.ChangeDutyCycle(60)
	return render_template('car.html')

###############倒车##################
@app.route("/back_a_car")
def back_a_car():
	GPIO.output(a2, GPIO.HIGH)
	GPIO.output(a1, GPIO.LOW)
	pwma1.ChangeDutyCycle(60)
	GPIO.output(b2, GPIO.HIGH)
	GPIO.output(b1, GPIO.LOW)
	pwmb1.ChangeDutyCycle(60)
	return render_template('car.html')



if __name__ == "__main__":
	app.run(host='192.168.43.101', port=80, debug=False)
	